﻿package ui.control.controlStruct
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import ui.GuiWnd;

	public class TagTreeNode extends GuiWnd
	{
		public var strName:String;
		public var fatherNode:TagTreeNode = null;
		public var vecNode:Vector.<TagTreeNode > = new Vector.<TagTreeNode> ;

		public var bdOpen:BitmapData;
		public var bdClose:BitmapData;

		public var btIco:Bitmap = null;

		public var btImg:Sprite;
		private var tfText:TextField;
		private var objData:Object;
		private var bOpen:Boolean = true;

		public function TagTreeNode()
		{
			tfText = new TextField();
			tfText.autoSize = TextFieldAutoSize.LEFT;

			tfText.width = 100;
			tfText.height = 18;
			
			tfText.mouseEnabled = false;

			addChild(tfText);
		}

		public function addItem(item:TagTreeNode, index:int = -1):void
		{
			if (index < -1)
			{
				index = -1;
			}
			if (index > vecNode.length)
			{
				index = vecNode.length;
			}
			if(index == -1)
			{
				vecNode.push(item);
			}
			else
			{
				vecNode.splice(index, 0, item);
			}

			openItem(isOpen);			
			
			updataItemPos();
		}

		public function deleteItem(item:TagTreeNode):void
		{
			if (! item)
			{
				return;
			}
			var index:int = vecNode.indexOf(item);
			if (index == -1)
			{
				return;
			}
			if (this.contains(item))
			{
				this.removeChild(item);
			}
			vecNode.splice(index, 1, item);

			updataItemPos();

		}

		public function set isOpen(b:Boolean):void
		{
			bOpen = b;
			openItem(bOpen);
		}

		public function get isOpen():Boolean
		{
			return bOpen;
		}

		private function openItem(b:Boolean):void
		{
			var len:int = vecNode.length;
			for (var i:int=0; i<len; i++)
			{
				var node:TagTreeNode = vecNode[i];

				if (b)
				{
					this.addChild(node);
				}
				else
				{
					if (this.contains(node))
					{
						this.removeChild(node);
					}
				}
			}
			if (btIco)
			{
				var img:BitmapData = bOpen ? bdOpen:bdClose;
				btIco.bitmapData = img;
			}


		}
		
		public function set htmlText(str:String):void
		{
			tfText.htmlText = str;
		}
		
		public function get htmlText():String
		{
			return tfText.text;
		}

		public function set data(obj:Object):void
		{
			objData = obj;
		}

		public function get data():Object
		{
			return objData;
		}

		public function setStateIco(OpenImg:BitmapData, CloseImg:BitmapData):void
		{
			bdOpen = OpenImg;
			bdClose = CloseImg;
			
			var img:BitmapData = bOpen ? bdOpen:bdClose;
			btIco = new Bitmap(img);

			updataItemPos();
		}
		
		private function showIco():void
		{
			if(!btIco) return;
			
			if(vecNode.length > 0 && !this.contains(btIco))
				addChild(btIco);
			else if(vecNode.length <= 0 && this.contains(btIco))
				removeChild(btIco);
		}
		
		private function get isShowIco():Boolean
		{
			if(!btIco) return false;
			
			return this.contains(btIco);
		}

		public function set img(img:Sprite):void
		{
			btImg = img;
			addChild(btImg);
			updataItemPos();
		}

		public function updataPos():void
		{
			showIco();
			
			var xx:int = 0;
			var yy:int = 0;

			if (isShowIco)
			{
				btIco.x = 0;
				btIco.y = 0;
				xx = btIco.width;
			}

			if (btImg)
			{
				btImg.x = xx;
				btImg.y = 0;
				xx = xx + btImg.width;
			}
			tfText.x = xx;
		}


		public function updataItemPos():void
		{
			updataPos();
			
			var xx:int = 0;
			var yy:int = tfText.height;
			
			if (isShowIco)
			{
				xx = btIco.width;
								
				if(btIco.width > yy)
					yy = btIco.width;
			}
			if (xx==0)
			{
				xx = 8;
			}
			
			if(btImg)
			{
				if(btImg.width > yy)
					yy = btImg.width;
			}

			var len:int = vecNode.length;
			for (var i:int=0; i<len; i++)
			{
				var node:TagTreeNode = vecNode[i];
				if(node.visible)
				{
					node.x = xx;
					node.y = yy;
					yy +=  node.height + 2;
				}
			}
		}

	}

}